//
//  PainterTextKLine.mm
//  ChartRefactor
//
//  Created by CaoJun on 15/3/17.
//  Copyright (c) 2015年 CaoJun. All rights reserved.
//

#import "PainterTextKLine.h"

using namespace std;
using namespace ytx;

@implementation PainterTextKLine

- (void) drawBottomSideText:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition
{
    KLineData * data = (KLineData *)indexPosition->getData();
    
    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();
    
    NSString * font = [NSString stringWithUTF8String:style.font.c_str()];
    CGFloat size = style.timeRangeSize;
    NSString * color = [NSString stringWithUTF8String:style.timeRangeColor.c_str()];
    
    CGPoint point;
    
    const vector<string> & bottomSideTextList(data->getBottomSideText(rect.getWidthType()));
    int startIndex = indexPosition->getStartDataIndex();
    
    string stringTemp = bottomSideTextList[startIndex];
    
    stringTemp.insert(4, "/").insert(7, "/");
    
    if (LineType::isMinuteLine(data->quotationType)) {
        stringTemp = stringTemp.erase(16,17);
    }
    else{
        stringTemp = stringTemp.erase(10, 10);
    }
    
    CGRect labelSize = [ctx getTextRect:[NSString stringWithUTF8String:stringTemp.c_str()] withFont: font withSize:size];
    
    CGFloat startEndWidth = labelSize.size.width;
    
    point.y = rect.getEndY() + 2;
    
    float allWidth = rect.getRectWidth();
    
    float insertWidth = allWidth - 2 * startEndWidth - (3 + rect.getZoomX()/2)*2;
    
    point.x = rect.getViewPxByViewIndex(0);
    
    [ctx fillText:[NSString stringWithUTF8String:stringTemp.c_str()] withX:point.x withY:point.y withColor: color withFont:font withSize:size withAnchor:LeftTop];
    if (startIndex >= bottomSideTextList.size()) return;
    stringTemp = bottomSideTextList[startIndex];

    if (LineType::isMinuteLine(data->quotationType))
    {
        stringTemp = stringTemp.substr(9,5);
        labelSize = [ctx getTextRect:[NSString stringWithUTF8String:stringTemp.c_str()] withFont:font withSize:size];
    }
    
    CGFloat middleWidth = labelSize.size.width;
    
    int diffx = 0;
    diffx = insertWidth / (middleWidth + 5);
    diffx = diffx > 5 ? 5 : diffx;
    CGFloat intervalWidth = insertWidth / diffx;
    float position = rect.getViewPxByViewIndex(0) + startEndWidth + 2;
    for (int i = 0; i<diffx; i++) {
        int dx = rect.getViewIndexByPX(position +(i+1)*intervalWidth-middleWidth)+ startIndex;
        point.x = rect.getViewPxByViewIndex(dx - startIndex);
        
        if (data->hasData(dx)) {
            stringTemp = bottomSideTextList[dx];
            stringTemp.insert(4, "/").insert(7, "/");
            if (LineType::isMinuteLine(data->quotationType)) {
                stringTemp = stringTemp.substr(11,5);
            }
            else{
                stringTemp = stringTemp.erase(10, 10);
            }
        }
        else{
            stringTemp = "";
        }
        [ctx fillText:[NSString stringWithUTF8String:stringTemp.c_str()] withX:point.x withY:point.y withColor: color withFont:font withSize:size withAnchor:LeftTop];
        
    }
    
    if (indexPosition->isRightEmpty()) {
        stringTemp = "";
    }
    else{
        stringTemp = bottomSideTextList[indexPosition->getEndDataIndexInRange()];
        
        stringTemp.insert(4, "/").insert(7, "/");
        if (LineType::isMinuteLine(data->quotationType)) {
            stringTemp = stringTemp.erase(16,17);
        }
        else{
            stringTemp = stringTemp.erase(10, 10);
        }
    }
    point.x = rect.getEndX();
    
    [ctx fillText:[NSString stringWithUTF8String:stringTemp.c_str()] withX:point.x withY:point.y withColor: color withFont:font withSize:size withAnchor:RightTop];

}

@end
